//
//  AppDelegate.m
//  Project two framework
//
//  Created by 张峥 on 16/7/21.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
#import "ZZConfig.h"
#import "ZPZNetRequest+appLogin.h"
#import "LUtilityTools.h"
@interface AppDelegate ()
@property (nonatomic, strong) RootViewController *rootVC;
@property (nonatomic, strong) NSString *resultCode;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    _rootVC = [[RootViewController alloc] init];
    self.window.rootViewController = _rootVC;
    [self.window makeKeyAndVisible];
    [self setupGuidePageView];
    return YES;
}


- (void)setupGuidePageView
{
    _loginImage = [[UIImageView alloc]initWithFrame:SCREEN_Bounds];
    _loginImage.image = [UIImage imageNamed:@"Default-Landscape.png"];
    [self.rootVC.view addSubview:_loginImage];
    
    _loginView = [[UIView alloc]initWithFrame:SCREEN_Bounds];
    _loginView.backgroundColor = [UIColor blackColor];
    [self.rootVC.view addSubview:_loginView];
    
    _loginTextFeild = [[UIImageView alloc]initWithFrame:CGRectMake(320, SCREEN_WIDTH + 90, 395, 500)];
    _loginTextFeild.image = [UIImage imageNamed:@"登录框_03.png"];
    _loginTextFeild.userInteractionEnabled = YES;
    [UIView animateWithDuration:1 animations:^{
        _loginView.alpha = 0.6;
        _loginTextFeild.frame = CGRectMake(320,  90, 395, 500);
    }];
    [self.rootVC.view addSubview:_loginTextFeild];
    
    _username = [[UITextField alloc]initWithFrame:CGRectMake(40, 110, 325, 50)];
    _username.clearButtonMode = UITextFieldViewModeWhileEditing;
    _username.text = @"khjl001";
    /** app 一运行时  就处于 username 编辑状态 */
    [_username becomeFirstResponder];
    _username.font = [UIFont systemFontOfSize:25];
    [self.loginTextFeild addSubview:_username];
    
    _password = [[UITextField alloc]initWithFrame:CGRectMake(40, CGRectGetMaxY(self.username.frame) + 75, 325, 50)];
    _password.text = @"khjl001";
    _password.secureTextEntry = YES;
    _password.clearButtonMode = UITextFieldViewModeWhileEditing;
    _password.font = [UIFont systemFontOfSize:25];
    [self.loginTextFeild addSubview:_password];
    
    _rememb = [UIButton buttonWithType:UIButtonTypeCustom];
    _rememb.frame = CGRectMake(40, 315, 25, 25);
    [_rememb setImage:[UIImage imageNamed:@"记住密码未点击_03.png"] forState:UIControlStateNormal];
    [_rememb setImage:[UIImage imageNamed:@"记住密码点击_03.png"] forState:UIControlStateSelected];
    [_rememb addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginTextFeild addSubview:_rememb];
    
    _login = [UIButton buttonWithType:UIButtonTypeCustom];
    _login.frame = CGRectMake(185, 350, 170, 50);
    [_login setBackgroundImage:[UIImage imageNamed:@"登录_03.png"] forState:UIControlStateNormal];
    [_login addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginTextFeild addSubview:_login];
    
    _loginShadow = [[UIImageView alloc]initWithFrame:CGRectMake(185, 400, 170, 10)];
    _loginShadow.image = [UIImage imageNamed:@"登录阴影_05.png"];
    [self.loginTextFeild addSubview:_loginShadow];
    
}

/** 记住密码的点击事件 */
- (void)chooseAction:(UIButton *)sender
{
    sender.selected = !sender.selected;
}

/** 登录的点击事件 */
- (void)loginAction:(UIButton *)sender
{
    if (self.username.text.length == 0 ) {
        //[LUtilityTools showAlertWithTitle:@"提示" message:@"用户名不能为空" otherTitle:@"确定"];
        [LUtilityTools showAlertWithTitle:@"提示" message: @"用户名不能为空" otherTitle:@"确定" target:self.rootVC];
    }
    if (self.password.text.length == 0 && self.username.text.length != 0) {
        [LUtilityTools showAlertWithTitle:@"提示" message: @"密码不能为空" otherTitle:@"确定" target:self.rootVC];
    }
    if (self.username.text.length != 0 && self.password.text.length != 0) {
        [ZPZNetRequest getappLoginUsername:self.username.text Password:self.password.text comletion:^(id object, BOOL success) {
            if (success) {
                if ([object[@"resultCode"] integerValue] == 0) {
                    [UIView animateWithDuration:0.5 animations:^{
                        self.loginImage.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
                        self.loginView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
                        self.loginTextFeild.frame = CGRectMake(320, 90 + SCREEN_HEIGHT, 395, 500);
                        self.rootVC.nameLabel.text = self.username.text;
                        self.rootVC.passwordLabel.text = self.password.text;
                    }];
                }else {
                    [LUtilityTools showAlertWithTitle:@"用户名或密码错误" message:@"" otherTitle:@"确定" target:self.rootVC];
                    
                }
                
            }
        }];

    }
}
    


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
